package com.qtenv.datasync.worker.chuanyi;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.qtenv.datasync.api.chuanyi.ChuanYiApi;
import com.qtenv.datasync.base.DataSyncer;
import com.qtenv.datasync.constant.AppConstants;
import com.qtenv.datasync.service.entity.TerminalHistoryData;
import com.qtenv.datasync.service.entity.Terminals_bf;
import com.qtenv.datasync.service.entity.chuanyi.CYTerminalHistoryData;
import com.qtenv.datasync.service.service.TerminalsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName CYDataSyncWorker
 * @Author CBL
 * @Date 2022/2/24 14:52
 **/
@Component
public class CYDataSyncWorker extends DataSyncer {
    @Autowired
    private TerminalsService terminalsService;

    public CYDataSyncWorker() {
        super("川仪");
    }

    @Override
    public List<TerminalHistoryData> getTerminalHistoryData(String tenantId, String strStartTime, String strEndTime) {
        List<Terminals_bf> terminalsList = terminalsService.findMasTerminalsByTenantAndSupplier(tenantId, AppConstants.CY_SUPPLIER_ID);
//        System.out.println(JSON.toJSONString(terminalsList));
        if (CollectionUtil.isNotEmpty(terminalsList)) {
            LocalDate now = LocalDate.now();
            String startTime = (strStartTime == null ? now.toString() + " 00:00:00" : strStartTime);
            String endTime = (strEndTime == null ? now.toString() + " 23:59:59" : strEndTime);

            String strData = ChuanYiApi.GetDeviceData(startTime, endTime);
            if (StrUtil.isNotEmpty(strData)) {
                return this.transform(tenantId, strData);
            }
        }

        return null;
    }

    @Override
    public List<TerminalHistoryData> getTerminalHistoryDataForTerminal(Terminals_bf terminals, String tenantId, String strStartTime, String strEndTime) {
        return super.getTerminalHistoryDataForTerminal(terminals, tenantId, strStartTime, strEndTime);
    }

    @Override
    public List<TerminalHistoryData> getTerminalHistoryData(String tenantId, List<String> terminalIdList, String strStartTime, String strEndTime) {
        return this.getTerminalHistoryData(tenantId, strStartTime, strEndTime);
    }

    private List<TerminalHistoryData> transform(String tenantId, String strData) {
        List<TerminalHistoryData> result = CollectionUtil.newArrayList();

        Set<String> warningSet = CollectionUtil.newHashSet();
        warningSet.add("Excitation_Alm");
        warningSet.add("Frequency_Alm");
        warningSet.add("Electric_Alm");
        warningSet.add("SmallSignal_Alm");
        warningSet.add("Reverse_Alm");
        warningSet.add("Positive_Alm");

        List<CYTerminalHistoryData> cyTerminalHistoryDataList = JSON.parseArray(strData, CYTerminalHistoryData.class);
        cyTerminalHistoryDataList.forEach(cyTerminalHistoryData -> {
            TerminalHistoryData terminalHistoryData = new TerminalHistoryData();
            BeanUtil.copyProperties(cyTerminalHistoryData, terminalHistoryData);
            terminalHistoryData.setTenant_id(tenantId);
            terminalHistoryData.setSupplierID(AppConstants.CY_SUPPLIER_ID);

            /*告警信息处理*/
            Map<String, Object> attrsMap = BeanUtil.beanToMap(terminalHistoryData);
            Map<String, Object> filteredMap = attrsMap.entrySet().stream().filter(map -> warningSet.contains(map.getKey()) && !map.getValue().toString().equalsIgnoreCase("False"))
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
            // 有无告警
            terminalHistoryData.setWarnFlag(filteredMap.size() > 0 ? 1 : 0);
            // 告警内容
            terminalHistoryData.setWarning(filteredMap.size() > 0 ? filteredMap.values().stream().map(Objects::toString).collect(Collectors.joining(",")) : null);

            result.add(terminalHistoryData);
        });

        return result;
    }

    @Override
    public List<TerminalHistoryData> doGetTerminalHistoryDataFromTerminal(Terminals_bf terminals, String tenantId, String tids, String strStartTime, String strEndTime) {
        return super.doGetTerminalHistoryDataFromTerminal(terminals, tenantId, tids, strStartTime, strEndTime);
    }

    @Override
    public List<TerminalHistoryData> transformFromTerminal(Terminals_bf terminals, String tenantId, String strData) {
        return super.transformFromTerminal(terminals, tenantId, strData);
    }

    public static void main(String[] args) {
//        CYDataSyncWorker worker = new CYDataSyncWorker();
//        System.out.println(JSON.toJSONString(worker.getTerminalHistoryData("2022-03-17 00:00:00", "2022-03-17 23:59:59"), true));

        // 川仪数据接口拉取当天数据-有数据

//        String sp = " 00:00:00";
//        LocalDate s = LocalDate.parse("2022-06-17");
//        String ep = " 23:59:59";
//        LocalDate e = LocalDate.parse("2022-06-22");
//        while (s.isBefore(e) || s.isEqual(e)) {
//            worker.syncTerminalHistoryData(s.toString() + sp, s.toString() + ep);
//            s = s.plusDays(1L);
//        }
    }
}
